<?

  function Sorry()
  {
    global $msg;
    throw new Exception($msg["Sorry"]);
  }


  function maskqoute($str)
  {
    return strtr($str, array("\""=>"&quot;"));
  }


  function current_time_asstr()
  {
    return timestamp_to_sql_datetime(time());
  }


  function timestamp_to_sql_datetime($timestamp)
  {
    return date( 'Y-m-d H:i:s', $timestamp );
  }

/*
  function datetime_to_date_time($datetime, &$date, &$time)
  {
    $date = "";
    $time = "";
    try
    {
      $dt = new DateTime($datetime);
      $date = $dt->format('d-m-Y');
      $time = $dt->format('H:i');
      $time = ($time <> "00:00") ? "[".$time."]" : "";
    }
    catch (Exception $e) 
    {
      $date = $datetime;
    };
  }
*/

  function datetime_to_date_time($datetime, &$date, &$time)
  {
    $ts = strtotime($datetime);
    if (($ts !== False) and ($ts <> -1))
    {
      $date = date( 'd-m-Y', $ts );
      $time = date( 'H:i', $ts );
    }
    else
    {
      $date = "";
      $time = "";
    }
  }


  function redirect_timeout($url, $timeout)
  {
  ?>  

  <script type="text/javascript">
    setTimeout('Redirect()',<?=$timeout;?>);
    function Redirect() {
      location.href='<?=$url;?>';
    }
  </script>
  
  <?php  
  }


  function check_admin_access()
  {
    global $msg;
    global $user;

    if (!$user["admin"]) 
    {
      echo "<br>", $msg["AccessDenied"], "<br><br>";
      return False;
    }

    return True;
  }


  function print_continue($message, $ref)
  {
    global $msg;

    print "$message<br>\n";
    print "<a href=\"$ref\">{$msg["Continue"]}</a>\n";
    redirect_timeout($ref, 1000);
  }


  function print_errors($errors) 
  {
    global $msg;

    $n = count($errors);

    print "{$msg["Error"]}<br>\n";
    print "<ul>\n";

    for ($i = 0; $i < $n; $i++) 
    {
      print "<li>$errors[$i]</li>\n";
    }

    print "</ul>\n";
  };


  function extract_lang_str($str, $alang = "")
  {
    global $lang;

    if (!$alang)
      {$alang = $lang;}

    $strs = preg_split("/<lang=['\"](\\w+)['\"]>/i", $str, 0, PREG_SPLIT_DELIM_CAPTURE);
    $res = $strs[0];

    $n = count($strs);
    for ($i = 1; $i < $n; $i+=2) 
    {
      if ($strs[$i] == $alang)
      {
        $res = $strs[$i + 1];
        break;
      }
    }

    return Trim($res);
  }


  function print_news($news)
  {
    datetime_to_date_time($news["date"], $date, $time);
    $time = ($time && ($time <> "00:00")) ? "[".$time."]" : "";

    print "<!-- news -->\n";
    print "<div class=\"news_date\"><b>$date</b> <small>$time</small></div>\n";
    print "<div class=\"news_text\">\n";
    print nl2br(extract_lang_str($news["text"]))."\n";
    print "</div>\n";
  }


  function print_user_info($user, $short = False)
  {
    global $msg;

    print "\n<!-- Profile info -->\n";

    print "<div class=\"user_info\">\n";
    print "<div class=\"user_info_in\">\n";

    print "<span class=\"info_prompt\">{$msg["UserName"]}</span>\n";
    print "<span class=\"info_text\">{$user["name"]}</span>\n";

    print "<span class=\"info_prompt\">{$msg["EMail"]}</span>\n";
    print "<span class=\"info_text\">{$user["email"]}</span>\n";

    if (!$short)
    {
      if ($user["fullname"] <> ""){
        print "<span class=\"info_prompt\">{$msg["FullName"]}</span>\n";
        print "<span class=\"info_text\">{$user["fullname"]}</span>\n";
      }

      if ($user["comment"] <> ""){
        print "<span class=\"info_prompt\">{$msg["Comment"]}</span>\n";
        print "<span class=\"info_comment\">\n";
        print nl2br(extract_lang_str($user["comment"]))."<br>\n";
        print "</span>\n";
      }
    }

    print "</div>\n";
    print "</div>\n";
  }
?>